<?php
define('ACTION_ADDGROUP', 'add');
define('ACTION_DELETEGROUP', 'del');
define('ACTION_LISTGROUP', 'list');

define('GROUPNAME_MIN', 3);

switch(true){
	case (strcmp($action, ACTION_ADDGROUP) == 0):
		if(!empty($_GET['name']) && is_string($_GET['name']) && strlen($_GET['name']) >= GROUPNAME_MIN){
			$groupName = $_GET['name'];
			$newGroup = $user->createGroup($groupName);
			$result = array('status'=> NO_ERR ,'result' => $newGroup->tojson());
		} else {
			$result = array('status' => ERR_GENERAL);
		}
		break;
	case (strcmp($action, ACTION_DELETEGROUP) == 0):
		if(!empty($_GET['gid'])){
			$idGroup = $_GET['gid'];
			$group = new Group();
		
			if($group->loadFromDB($idGroup)) {
				$user->deleteGroup($group);
				$result = array('status'=> NO_ERR);
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
			
		}
		break;
	case (strcmp($action, ACTION_LISTGROUP) == 0):
		$listGroups = $user->listGroups();
		$result = array('status'=> NO_ERR ,'result' => $listGroups);
		break;
	default:
		$result = array('status' => ERR_GENERAL);
}

?>